package drukarnia.view;

import java.io.File;

import java.io.IOException;

import java.util.Iterator;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.FileUploadException;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import drukarnia.controller.FTPManager;
import drukarnia.controller.ImageManager;
import drukarnia.controller.SizeInM2Manager;

@SuppressWarnings("serial")
public class Upload extends HttpServlet {

    @SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String mail = request.getParameter("mail");
    	String nazwaAlbumu = mail + "Album";
    	FTPManager ftp = new FTPManager();
    	ImageManager image = new ImageManager();
    	SizeInM2Manager size = new SizeInM2Manager();
    	String savePath = this.getServletConfig().getServletContext().getRealPath("");
        savePath = savePath + "/uploads/";
        File f1 = new File(savePath);
        if (!f1.exists()) {
            f1.mkdirs();
        }
        
        DiskFileItemFactory fac = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(fac);
        ftp.polaczZFTP();
        ftp.wybierzKatalogFTP("/druk/"+nazwaAlbumu);
        upload.setHeaderEncoding("utf-8");

        List fileList = null;
        try {
            fileList = upload.parseRequest(request);
        } catch (FileUploadException ex) {
            return;
        }

        Iterator<FileItem> it = fileList.iterator();
        String name = "";
        String extName = "";
        while (it.hasNext()) {
            FileItem item = it.next();
            if (!item.isFormField()) {
                name = item.getName();
                String type = item.getContentType();
                if (name == null || name.trim().equals("")) {
                    continue;
                }
                File saveFile = new File(savePath + name );
                
                try {
                    item.write(saveFile);
//                    if(saveFile.getName().contains(".tif") || saveFile.getName().contains(".tiff") || saveFile.getName().contains(".TIF") || saveFile.getName().contains(".TIFF")){
//                    	com.lowagie.text.Image img = com.lowagie.text.Image.getInstance(saveFile.getPath());
//    					System.out.println("X: " + img.getWidth() + " DPI: X: " + img.getDpiX() );
//    					System.out.println("Y: " + img.getHeight() + " DPI: Y: " + img.getDpiY());
//                        double sizeInM2 = image.getSizeInM2(img.getWidth(), img.getHeight(), img.getDpiX(), img.getDpiY());
//                        size.save(sizeInM2, saveFile.getName());
//                        System.out.println("FINAL!!!!: " + sizeInM2);
//                    	
//                    }
//                    else{
//                    	System.out.println("Nie tif");
//                    }
                    ftp.wrzucNaSerwer(saveFile, "");
                    
                } catch (Exception e) {
                    e.printStackTrace();

                }

            }

        }
        ftp.rozlaczZFTP();
        response.getWriter().print(name + extName);

    }

}
